package cn.itcast.blog.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.itcast.blog.Utils.SessionFactoryUtils;
import cn.itcast.blog.domain.AjaxResponse;
import cn.itcast.blog.domain.Post;
import cn.itcast.blog.mapper.PostMapper;

/**
 * 具体实现Post代码逻辑
 * @author 16604
 *
 */
public class PostService{
	
	PostMapper mapper;
	
	//查询所有文章
	public List<Post> findByAll() {
		
		List<Post> findByAll = mapper.findByAll();
		
		return findByAll;
		
	}
	
	//查询所有文章并把文章所在的分类查询出来
	public List<Post> findByAllWithCategory() {
		
		//取到分页参数，查询对应页的数据
		List<Post> findByAllWithCategory = mapper.findByAllWithCategory();
		
		return findByAllWithCategory;
	}

	public AjaxResponse<Post> findPage(int start, int size) {
		
		List<Post> page = mapper.findPage((start-1)*size, size-1);
		
		int findCount = mapper.findCount();
		
		AjaxResponse<Post> ajaxResponse = new AjaxResponse<>();
		ajaxResponse.setCount(findCount);
		ajaxResponse.setData(page);
		ajaxResponse.setCode(0);
		ajaxResponse.setMsg("");
		
		return ajaxResponse;
	}
	
}
